package com.file.system.controller.business;


import com.file.system.entity.res.CommonResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.file.system.tools.regular.RegularUtil;

import org.springframework.web.bind.annotation.PostMapping;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.file.system.entity.res.DataPage;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

import static cn.hutool.core.util.ObjectUtil.isNotEmpty;
import static cn.hutool.core.util.ObjectUtil.isNotNull;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;

import com.file.system.service.business.IBusUtensilIndentService;
import com.file.system.entity.business.BusUtensilIndent;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 器具订单
 *
 * @author jch
 */
@RestController
@Validated
@RequestMapping("/bus/utensil/indent")
public class BusUtensilIndentController {

    @Autowired
    private IBusUtensilIndentService service;

    /**
     * 查询器具订单分页
     *
     * @param pageIndex 页数
     * @param pageSize  每页条数
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/page/{pageIndex}/{pageSize}")
    public DataPage<List<BusUtensilIndent>> page(@PathVariable
                                                               @Min(value = 0, message = "页数最小1")
                                                               Integer pageIndex,
                                                               @PathVariable
                                                               @Min(value = 1, message = "每页条数最小1")
                                                               @Max(value = 100, message = "每页条数最大100")
                                                               Integer pageSize,
                                                               @Validated BusUtensilIndent busUtensilIndent
    ) {
        QueryWrapper<BusUtensilIndent> query = new QueryWrapper<>();
        query.orderByDesc("a.id");
        query.eq("a.is_del", 0);
        query.like(StrUtil.isNotEmpty(busUtensilIndent.getRegFormNo()), "a.reg_form_no", busUtensilIndent.getRegFormNo());
        query.like(StrUtil.isNotEmpty(busUtensilIndent.getNameOfEntrustOrg()), "a.name_of_entrust_org", busUtensilIndent.getNameOfEntrustOrg());
        query.like(StrUtil.isNotEmpty(busUtensilIndent.getAddressOfEntrustOrg()), "a.address_of_entrust_org", busUtensilIndent.getAddressOfEntrustOrg());
        query.like(StrUtil.isNotEmpty(busUtensilIndent.getContactPersonOfEntrustOrg()), "a.contact_person_of_entrust_org", busUtensilIndent.getContactPersonOfEntrustOrg());
        query.like(StrUtil.isNotEmpty(busUtensilIndent.getPhoneOfEntrustOrg()), "a.phone_of_entrust_org", busUtensilIndent.getPhoneOfEntrustOrg());
        query.like(StrUtil.isNotEmpty(busUtensilIndent.getCertificationCompany()), "a.certification_company", busUtensilIndent.getCertificationCompany());
        query.like(StrUtil.isNotEmpty(busUtensilIndent.getAddress()), "a.address", busUtensilIndent.getAddress());
        query.like(StrUtil.isNotEmpty(busUtensilIndent.getLinkman()), "a.linkman", busUtensilIndent.getLinkman());
        query.like(StrUtil.isNotEmpty(busUtensilIndent.getLinkPhone()), "a.link_phone", busUtensilIndent.getLinkPhone());
        query.like(StrUtil.isNotEmpty(busUtensilIndent.getAccepterSign()), "a.accepter_sign", busUtensilIndent.getAccepterSign());
        query.like(StrUtil.isNotEmpty(busUtensilIndent.getSignContent()), "a.sign_content", busUtensilIndent.getSignContent());
        query.like(StrUtil.isNotEmpty(busUtensilIndent.getDictSignStatus()), "a.dict_sign_status", busUtensilIndent.getDictSignStatus());

        pageIndex = pageIndex + 1;
        //查询集合
        DataPage<List<BusUtensilIndent>> dp = service.page(pageIndex, pageSize, query);
        return dp;
    }

    /**
     * 通过ids查询器具订单集合
     *
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/listByIds")
    public CommonResult<List<BusUtensilIndent>> listByIds(@RequestBody List<Long> ids) {
        //查询集合
        List<BusUtensilIndent> list = service.listByIds(ids);
        return CommonResult.ok().setResult(list);
    }

    /**
     * 查询所有非删除状态器具订单
     *
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/listAll")
    public CommonResult<List<BusUtensilIndent>> listAll() {
        //查询集合
        List<BusUtensilIndent> list = service.listAll();
        return CommonResult.ok().setResult(list);
    }


    /**
     * 通过id查询器具订单
     *
     * @param id 器具订单id
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/id/{id:" + RegularUtil.ID + "}")
    public CommonResult<BusUtensilIndent> getById(@PathVariable
                                                  @Min(value = 1, message = "id长度最小1")
                                                  Long id) {
        //查询集合
        BusUtensilIndent busUtensilIndent = service.getById(id);
        return CommonResult.ok().setResult(busUtensilIndent);
    }

    /**
     * 通过id删除器具订单
     *
     * @param id id
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/remove/{id:" + RegularUtil.ID + "}")
    public CommonResult removeById(@PathVariable
                                   @Min(value = 1, message = "id长度最小1")
                                   Long id) {
        //返回
        return CommonResult.check(service.removeById(id));
    }

    /**
     * 通过ids删除器具订单
     *
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/removeByIds")
    public CommonResult removeById(@RequestBody List<Long> ids) {
        //返回
        return CommonResult.check(service.removeByIds(ids));
    }


    /**
     * 通过id编辑器具订单
     *
     * @param busUtensilIndent 器具订单
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/updateById")
    public CommonResult<BusUtensilIndent> updateById(@RequestBody @Validated(BusUtensilIndent.update.class) BusUtensilIndent busUtensilIndent) {
        //编辑修改
        return CommonResult.check(service.updateById(busUtensilIndent)).setResult(busUtensilIndent);
    }


    /**
     * 添加器具订单
     *
     * @param busUtensilIndent 器具订单
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/save")
    public CommonResult<BusUtensilIndent> save(@RequestBody @Validated(BusUtensilIndent.add.class) BusUtensilIndent busUtensilIndent) {
        //添加器具订单
        return CommonResult.check(service.save(busUtensilIndent)).setResult(busUtensilIndent);
    }


    /**
     * 添加器具订单
     *
     * @param busUtensilIndent 器具订单
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/sign")
    public CommonResult<BusUtensilIndent> sign(@RequestBody @Validated(BusUtensilIndent.sign.class) BusUtensilIndent busUtensilIndent) {
        //添加器具订单
        return CommonResult.check(service.sign(busUtensilIndent)).setResult(busUtensilIndent);
    }
}

